
#include "KyotoImageSet.h"

void datasets_kyoto_image_set::CImageSet::myInit(
	string strPN_ImageBase,
	string strFN_ImageList)
{
	int i;
	char strTemp[10001];

	this->strPN_Base = strPN_ImageBase;

	ifstream inFile(strFN_ImageList.c_str());
	inFile>>AmountImage;
	inFile.getline(strTemp, 10000);
	vStrFN_Image.Clear();
	for ( i = 0; i < AmountImage; ++i )
	{
		inFile.getline(strTemp, 10000);
		vStrFN_Image.PushBack(strTemp);
	}
	inFile.close();
	inFile.clear();
}

bool datasets_kyoto_image_set::CImageSet::GetImage( int ImageIndex, bool fColored, CImg<int>& imgTarget )
{
	if (ImageIndex<0 || ImageIndex>=AmountImage) return false;
	char strTemp[10001];
	int W, H, i, ii, i1, i2;
	double tc;
	int rc;
	sprintf_s(strTemp, "%s%s", strPN_Base.c_str(), vStrFN_Image.pElement[ImageIndex].c_str());
	ifstream inFile(strTemp);
	inFile>>H>>W;
	imgTarget.assign(W, H, 1, (fColored ? 3 : 1));
	ii = W * H * 3; i1 = W * H * 1; i2 = W * H * 2;
	for ( i = 0; i < ii; ++i )
	{
		inFile>>tc;
		rc = (int)(tc * 256.0); rc = (rc<0 ? 0 : (rc>255 ? 255 : rc));
		if (fColored)
			imgTarget.data[i] = rc;
		else if (i>=i1 && i<i2)
			imgTarget.data[i-i1] = rc;
	}
	return true;
}

bool datasets_kyoto_image_set::CImageSet::GetImage( int ImageIndex, bool fColored, CImg<double>& imgTarget )
{
	if (ImageIndex<0 || ImageIndex>=AmountImage) return false;
	char strTemp[10001];
	int W, H, i, ii, i1, i2;
	double tc;
	sprintf_s(strTemp, "%s%s", strPN_Base.c_str(), vStrFN_Image.pElement[ImageIndex].c_str());
	ifstream inFile(strTemp);
	inFile>>H>>W;
	imgTarget.assign(W, H, 1, (fColored ? 3 : 1));
	ii = W * H * 3; i1 = W * H * 1; i2 = W * H * 2;
	for ( i = 0; i < ii; ++i )
	{
		inFile>>tc;
		if (fColored)
			imgTarget.data[i] = tc;
		else if (i>=i1 && i<i2)
			imgTarget.data[i-i1] = tc;
	}
	ii = imgTarget.size();
	for ( i = 0; i < ii; ++i )
	{
		inFile>>tc;
		imgTarget.data[i] = tc;
	}
	return true;
}

bool datasets_kyoto_image_set::CImageSet::GetImage( int ImageIndex, bool fColored, CImg<float>& imgTarget )
{
	if (ImageIndex<0 || ImageIndex>=AmountImage) return false;
	char strTemp[10001];
	int W, H, i, ii, i1, i2;
	float tc;
	sprintf_s(strTemp, "%s%s", strPN_Base.c_str(), vStrFN_Image.pElement[ImageIndex].c_str());
	ifstream inFile(strTemp);
	inFile>>H>>W;
	imgTarget.assign(W, H, 1, (fColored ? 3 : 1));
	ii = W * H * 3; i1 = W * H * 1; i2 = W * H * 2;
	for ( i = 0; i < ii; ++i )
	{
		inFile>>tc;
		if (fColored)
			imgTarget.data[i] = tc;
		else if (i>=i1 && i<i2)
			imgTarget.data[i-i1] = tc;
	}
	ii = imgTarget.size();
	for ( i = 0; i < ii; ++i )
	{
		inFile>>tc;
		imgTarget.data[i] = tc;
	}
	return true;
}

